package com.dykj.fireFighting.web.xfyl;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.xfyl.dto.EmergencyOrganizeDto;
import com.dykj.fireFighting.api.socialUnit.xfyl.entity.EmergencyOrganize;
import com.dykj.fireFighting.api.socialUnit.xfyl.service.IEmergencyOrganizeService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 应急组织接口
 *
 * @Author JacksonTu
 * @Date  2021-07-26 13:42:55
 */
@Slf4j
@Api(value = "应急组织接口", tags = {"应急组织接口"})
@RestController
@RequestMapping("/emergencyOrganize")
public class EmergencyOrganizeController {

    @DubboReference
    private IEmergencyOrganizeService emergencyOrganizeService;


    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }


    /**
     * 应急组织列表
     *
     * @return
     */
    @AutoLog(value = "应急组织-分页列表查询")
    @ApiOperation(value = "应急组织分页列表", notes = "应急组织分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<EmergencyOrganize>> treeGrid(EmergencyOrganizeDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<EmergencyOrganize> page = emergencyOrganizeService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取应急组织信息
     */
    @AutoLog(value = "应急组织-获取应急组织信息")
    @ApiOperation(value = "获取应急组织信息", notes = "获取应急组织信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<EmergencyOrganize> info(@PathVariable("id") String id){
        EmergencyOrganize emergencyOrganize = emergencyOrganizeService.getById(id);
        return CommonResult.success(emergencyOrganize);
    }

    /**
     * 保存应急组织信息
     */
    @AutoLog(value = "应急组织-保存应急组织信息")
    @ApiOperation(value = "保存应急组织信息", notes = "保存应急组织信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody EmergencyOrganize emergencyOrganize) {
        try {
            emergencyOrganize.setCreateTime(new Date());
            emergencyOrganize.setCreateBy(getName());
            emergencyOrganize.setUnitId(getEnterpriseId());
                emergencyOrganizeService.save(emergencyOrganize);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改应急组织信息
     */
    @AutoLog(value = "应急组织-修改应急组织信息")
    @ApiOperation(value = "修改应急组织信息", notes = "修改应急组织信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody EmergencyOrganize emergencyOrganize) {
        try {
            emergencyOrganize.setUpdateTime(new Date());
            emergencyOrganize.setUpdateBy(getName());
                emergencyOrganizeService.updateById(emergencyOrganize);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除应急组织信息
     */
    @AutoLog(value = "应急组织-删除应急组织信息")
    @ApiOperation(value = "删除应急组织信息", notes = "删除应急组织信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                emergencyOrganizeService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
